package com.siwei.mes.service.laboratory.factory.material.cement;

import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.common.util.NullUtils;
import com.siwei.mes.entity.experiment.CheckBatchConfig;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentDetail;
import com.siwei.mes.entity.experiment.cement.CementNjsj;
import com.siwei.mes.entity.laboratory.cement.DataSnBZCD_NJSJ;
import com.siwei.mes.entity.system.TestProjectInfo;
import com.siwei.mes.entity.system.TestProjectSpecConfig;
import com.siwei.mes.enums.TestProjectEnum;
import com.siwei.mes.mapper.experiment.ExperimentDetailMapper;
import com.siwei.mes.mapper.laboratory.CementLaboratoryMapper;
import com.siwei.mes.service.laboratory.factory.LaboratoryAbstractService;
import com.siwei.mes.service.laboratory.factory.entity.LaboratoryDownloadVo;
import com.siwei.mes.service.laboratory.factory.material.MaterialLaboratoryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 水泥-凝结时间
 *
 * @Description: 水泥-凝结时间
 * @Version: 1.0
 * @author: mlchen
 * @date: 2025/3/29 17:07
 */
@Service
public class CementNjsjFactory extends LaboratoryAbstractService implements MaterialLaboratoryFactory {
    private static final Logger log = LoggerFactory.getLogger("laboratory");

    @Resource
    private CementLaboratoryMapper cementLaboratoryMapper;
    @Resource
    private ExperimentDetailMapper experimentDetailMapper;

    private final TestProjectEnum testProjectEnum = TestProjectEnum.CEMENT_PARAM_NJSJ;

    @Override
    public void processUpload(JSONObject objJson, String sampleNo, Experiment experiment, List<CheckBatchConfig> batchConfigList, List<TestProjectInfo> testProjectInfoList, List<TestProjectSpecConfig> testProjectSpecConfigList, ExperimentDetail detail, String samId) {
        log.info("开始上传到协会数据库：【水泥-{}】", testProjectEnum.getName());
        //将json转成对象
        CementNjsj cementNjsj = JsonUtils.getObject(objJson, CementNjsj.class);

        if (cementNjsj == null) {
            log.info("水泥-【{}】数据为空，跳过上传！objJson：{}", testProjectEnum.getName(), objJson);
            return;
        }

        //创建协会水泥标准稠度合凝结时间对象
        DataSnBZCD_NJSJ dataSnBZCD_njsj = new DataSnBZCD_NJSJ();

        dataSnBZCD_njsj.setTestMethod("标准法");

        if (cementNjsj.getNjsj() != null) {
            dataSnBZCD_njsj.setS10(NullUtils.ifNull(cementNjsj.getNjsj().getXcsd()));
            dataSnBZCD_njsj.setS5(NullUtils.ifNull(cementNjsj.getNjsj().getDdcnsj()));
            dataSnBZCD_njsj.setS7(NullUtils.ifNull(cementNjsj.getNjsj().getDdznsj()));
            dataSnBZCD_njsj.setS9(NullUtils.ifNull(cementNjsj.getNjsj().getJssj()));
        }
        if (cementNjsj.getJgjs() != null) {
            dataSnBZCD_njsj.setS6(NullUtils.ifNull(cementNjsj.getJgjs().getCnsj()));
            dataSnBZCD_njsj.setS8(NullUtils.ifNull(cementNjsj.getJgjs().getZnsj()));
            dataSnBZCD_njsj.setConclusion(NullUtils.ifNull(cementNjsj.getJgjs().getCnsjjl()));
            dataSnBZCD_njsj.setConclusion2(NullUtils.ifNull(cementNjsj.getJgjs().getZnsjjl()));
        }

        dataSnBZCD_njsj.setST1("45");
        dataSnBZCD_njsj.setST2("600");

        //设置公共属性
        super.setCommonProperties(sampleNo, experiment, dataSnBZCD_njsj, "水泥", cementNjsj.getJcrq(), detail);

        //根据样品编码查询水泥标准稠度与初凝时间
        DataSnBZCD_NJSJ data_jgcl_sn_bzcdnjsjBySampleNo = cementLaboratoryMapper.findDATA_JGCL_SN_BZCDNJSJBySampleNo(sampleNo);
        //查询没值说明需要  新增水泥标准稠度与初凝时间
        if (data_jgcl_sn_bzcdnjsjBySampleNo == null) {
            cementLaboratoryMapper.addDATA_JGCL_SN_BZCDNJSJ(dataSnBZCD_njsj);
        } else {
            //修改水泥标准稠度与初凝时间
            cementLaboratoryMapper.updateDATA_JGCL_SN_BZCDNJSJ(dataSnBZCD_njsj);
        }
    }

    @Override
    public LaboratoryDownloadVo processDownload(String sampleNo, Experiment experiment, ExperimentDetail detail, Map<String, String> parameterMap, String samId, List<TestProjectInfo> testProjectInfoList) {
        log.info("开始下载协会数据到品控：【水泥-{}】", testProjectEnum.getName());
        JSONObject objJson = detail.getObjJson();
        //将json转成对象
        CementNjsj cementNjsj = JsonUtils.getObject(objJson, CementNjsj.class);
        if (cementNjsj == null) {
            log.info("【水泥-{}】数据为空，跳过下载！objJson：{}", testProjectEnum.getName(), objJson);
            return null;
        }

        //根据样品编码查询水泥标准稠度与初凝时间
        DataSnBZCD_NJSJ data = cementLaboratoryMapper.findDATA_JGCL_SN_BZCDNJSJBySampleNo(sampleNo);

        if (data == null) {
            log.info("查询协会【水泥-{}】信息为空。跳过下载！sampleNo：【{}】", testProjectEnum.getName(), sampleNo);
            return null;
        }

        CementNjsj.Njsj njsj = cementNjsj.getNjsj();
        CementNjsj.Jgjs jgjs = cementNjsj.getJgjs();

        if (njsj != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                if (data.getS5() != null) {
                    String s5 = data.getS5();
                    Date s5Date = sdf.parse(s5);
                    String strDate = sdf.format(s5Date);
                    njsj.setDdcnsj(strDate);
                    if (jgjs != null) {
                        jgjs.setCnsj(strDate);
                    }
                }
                if (data.getS7() != null) {
                    String s7 = data.getS7();
                    Date s7Date = sdf.parse(s7);
                    String strDate = sdf.format(s7Date);
                    njsj.setDdznsj(strDate);
                    if (jgjs != null) {
                        jgjs.setZnsj(strDate);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException("日期转换错误！" + e.getMessage());
            }

            if (data.getS10() != null) {
                njsj.setXcsd(data.getS10());
            }
        }

        if (jgjs != null) {
            if (data.getS6() != null) {
                jgjs.setCnsj(data.getS6());
            }
            if (data.getS8() != null) {
                jgjs.setZnsj(data.getS8());
            }
            if (data.getConclusion() != null) {
                jgjs.setCnsjjl(data.getConclusion());
            }
            if (data.getConclusion2() != null) {
                jgjs.setZnsjjl(data.getConclusion2());
            }

        }
        if (data.getDetectonDate() != null) {
            Date detectonDate = data.getDetectonDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String strDate = sdf.format(detectonDate);
            cementNjsj.setJcrq(strDate);
        }

        //对象转json
        JSONObject jsonObject = JsonUtils.beanToObject(cementNjsj);

        //重新赋值
        detail.setObjJson(jsonObject);

        detail.setEquipment(NullUtils.ifNull(data.getEquipment()));
        detail.setTemperature(NullUtils.ifNull(data.getTemperature()));
        detail.setHumidness(NullUtils.ifNull(data.getHumidity()));
        detail.setExperimentGist(NullUtils.ifNull(data.getDetectonRuleCode()));
        detail.setJudgeGist(NullUtils.ifNull(data.getAccessRuleCode()));

        if (parameterMap != null && parameterMap.get(detail.getTestProjectCode()) != null) {
            detail.setStatus(parameterMap.get(detail.getTestProjectCode()));
        }

        //修改品控试验数据
        experimentDetailMapper.updateByPrimaryKeySelective(detail);

        return new LaboratoryDownloadVo().setEquipment(detail.getEquipment())
                .setJudgeGist(detail.getJudgeGist())
                .setExperimentGist(detail.getExperimentGist())
                .setTemperature(detail.getTemperature())
                .setHumidness(detail.getHumidness());
    }
}
